package edu.xlh.travel.service.impl;

import com.alibaba.fastjson.JSON;
import edu.xlh.travel.common.EchartInRedisConstant;
import edu.xlh.travel.common.Result;
import edu.xlh.travel.pojo.User;
import edu.xlh.travel.service.EchartService;
import edu.xlh.travel.service.OnlineUserInfoService;
import edu.xlh.travel.service.OrdersService;
import edu.xlh.travel.service.UserService;
import edu.xlh.travel.redis.utils.JedisManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class EchartServiceImpl implements EchartService {
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersService orderService;
    @Autowired
    private OnlineUserInfoService onlineUserInfoService;
    @Autowired
    private JedisManager jedisManager;

    private String readEchartDataFromRedisKey = EchartInRedisConstant.ECHART_RD_DATA+"::"
            +EchartInRedisConstant.DATACOUNTREAD_RD_LIST;

    @Override
    public Result getRealTimeData(HttpSession session) {
        User user = (User)session.getAttribute("admin");
        if (user == null) {
            return new Result(201);
        }
        Integer userConut = userService.getUserCount();
        Integer orderConut = orderService.getOrderCount();
        Integer onlineUserConut = onlineUserInfoService.getOnlineUserCount();
        Map dataCount = new HashMap();
        dataCount.put("userCount" , userConut);
        dataCount.put("orderCount" , orderConut);
        dataCount.put("onlineUserCount" , onlineUserConut);
        return new Result(200 , "获取实时数据成功" , dataCount);
    }

    @Override
    public Result getLastWeekReport(HttpSession session) {
        User user = (User)session.getAttribute("admin");
        if (user == null) {
            return new Result(201);
        }
        List<String> OrignLastWeekDataCountListString = jedisManager.lrange(readEchartDataFromRedisKey, 0, 6);
        Map<String , List> lastWeekDataCountMap = new HashMap<>();
        List<Integer> lastWeekUserDataCountList = new ArrayList();
        List<Integer> lastWeekOrderDataCountList = new ArrayList();
        List<Integer> lastWeekonlineUserDataCountList = new ArrayList();
                OrignLastWeekDataCountListString.stream().forEach(item -> {
            //一天的不同种类的数据
            HashMap hashMap = JSON.parseObject(item, HashMap.class);
                    Integer userCount = (Integer)hashMap.get("用户数量");
                    Integer orderCount = (Integer)hashMap.get("订单数量");
                    Integer onlineUserCount = (Integer)hashMap.get("在线用户");
                    lastWeekUserDataCountList.add(userCount);
                    lastWeekOrderDataCountList.add(orderCount);
                    lastWeekonlineUserDataCountList.add(onlineUserCount);
        });
        lastWeekDataCountMap.put("userCount" , lastWeekUserDataCountList);
        lastWeekDataCountMap.put("orderCount" , lastWeekOrderDataCountList);
        lastWeekDataCountMap.put("onlineUserCount" , lastWeekonlineUserDataCountList);
        System.out.println(lastWeekDataCountMap);
        return new Result(200 , "上周数据统计返回成功" , lastWeekDataCountMap);
    }
}
